package servlet;

import dao.MembersInfoDao;
import entity.MembersInfo;
import service.MembersInfoService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/MembersInfoServlet")
public class MembersInfoServlet extends HttpServlet {

    private MembersInfoService membersInfoService;

    public MembersInfoServlet() {
        membersInfoService = new MembersInfoService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if ("all".equals(method)) {
            getAllMembers(request, response);
        } else if ("get".equals(method)) {
            getMemberById(request, response);
        } else if ("delete".equals(method)) {
            deleteMember(request, response);
        } else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if ("update".equals(method)) {
            updateMember(request, response);
        } else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }

    private void getAllMembers(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            List<MembersInfo> members = membersInfoService.getAllMembers();
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            out.print("[");
            for (int i = 0; i < members.size(); i++) {
                MembersInfo member = members.get(i);
                out.print("{");
                out.print("\"mid\": " + member.getMid() + ", ");
                out.print("\"mname\": \"" + member.getMname() + "\", ");
                out.print("\"mgender\": \"" + member.getMgender() + "\", ");
                out.print("\"mage\": " + member.getMage() + ", ");
                out.print("\"maddress\": \"" + member.getMaddress() + "\", ");
                out.print("\"memail\": \"" + member.getMemail() + "\"");
                if (i < members.size() - 1) {
                    out.print(",");
                }
                out.print("}");
            }
            out.print("]");
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

    private void getMemberById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idStr = request.getParameter("id");
        int id = Integer.parseInt(idStr);

        try {
            MembersInfo member = membersInfoService.getMemberById(id);
            if (member != null) {
                response.setContentType("application/json");
                response.setCharacterEncoding("UTF-8");
                PrintWriter out = response.getWriter();
                out.print("{");
                out.print("\"mid\": " + member.getMid() + ", ");
                out.print("\"mname\": \"" + member.getMname() + "\", ");
                out.print("\"mgender\": \"" + member.getMgender() + "\", ");
                out.print("\"mage\": " + member.getMage() + ", ");
                out.print("\"maddress\": \"" + member.getMaddress() + "\", ");
                out.print("\"memail\": \"" + member.getMemail() + "\"");
                out.print("}");
                out.flush();
            } else {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }

    private void updateMember(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String mname = request.getParameter("mname");
        String mgender = request.getParameter("mgender");
        int mage = Integer.parseInt(request.getParameter("mage"));
        String maddress = request.getParameter("maddress");
        String memail = request.getParameter("memail");

        MembersInfo member = new MembersInfo(id, mname, mgender, mage, maddress, memail);

        try {
            boolean isUpdated = membersInfoService.updateMember(member);
            if (isUpdated) {
                response.setStatus(HttpServletResponse.SC_OK);
                response.sendRedirect("Members.jsp");
            } else {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

    private void deleteMember(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idStr = request.getParameter("id");
        int id = Integer.parseInt(idStr);

        try {
            boolean isDeleted = membersInfoService.deleteMember(id);
            if (isDeleted) {
                response.setStatus(HttpServletResponse.SC_OK);
                response.sendRedirect("Members.jsp");
            } else {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }
}

